package  
{
	import com.increpare.bfxr.dataClasses.ItemData;
	import flash.geom.Rectangle;
	import org.flixel.*
	/**
	 * ...
	 * @author jfields
	 */
	public class Boss extends FlxSprite
	{
		[Embed (source = "../data/boss.png")] private var img_boss1:Class;
		[Embed (source = "../data/boss3.png")] private var img_boss2:Class;
		[Embed (source = "../data/boss2.png")] private var img_boss3:Class;
		
		public var turret:Turret;
		
		public var maxHealth:Number;
		public var pattern:BossPattern;
		public var states:Array;
		public var stateTransition:Number = 0;
		public var explosionTimer:Number = 0;
		
		public var moveSpeed:Number = 20;
		
		public var dying:Boolean = false;
		
		public var eyeX:Number = 43.5;
		public var eyeY:Number = 33.5;
		public var eyeDistance:Number = 16;
		
		public var thrusterTimer:Number = 0;
		public var gotHit:Boolean = false;
		
		public var dishIndex:int = 0;
		
		public function Boss(x:int, y:int, bossNum:int) 
		{
			super(x, y);
			if (bossNum == 0) { loadGraphic(img_boss1, true, false, 105, 80); }
			if (bossNum == 1) { loadGraphic(img_boss2, true, false, 105, 80); eyeY = 28.5;}
			if (bossNum == 2) { loadGraphic(img_boss3, true, false, 105, 80); eyeY = 30.5; }
			
			states = new Array();
			health = 1000
			maxHealth = 1000;
			velocity.x = -20;
		}
		
		override public function update():void
		{
			if (pattern)
			{
				pattern.update();
			}
			if (gotHit)
			{
				gotHit = false;
				frame = 1;
			}
			else
			{
				frame = 0;
			}
			if (dying)
			{
				velocity.y = -20;
				velocity.x = 0;
			}
			super.update();
			if (stateTransition > 0)
			{
				FlxG.shake(0.01, 0.1);
				stateTransition -= FlxG.elapsed;
				explosionTimer -= FlxG.elapsed;
				if (explosionTimer < 0)
				{
					GameState.ref.makeParticle(Particle.EXPLOSION, Math.random() * width + x, Math.random() * height + y);
					explosionTimer = 0.1;
					//if (Math.random() > 0.5)
					//{
						SoundAssets.bossExplosion.Play();
					//}
				}
			}
			if (health <= 0 && stateTransition < 0)
			{
				if (states.length > 1)
				{
					states.shift();
					health = (states[0] as BossState).health;
					maxHealth = health;
					pattern = (states[0] as BossState).pattern;
					pattern.patternStart();
					Saving.checkpointPlayerHealth = GameState.ref.player.health;
					dishIndex++;
				}
				else
				{
					//kill();
					FlxG.fade(0xff000000, 1, GameState.nextLevel);
				}
			}
			
			if (!dying && y < 10) { y += FlxG.elapsed * 20; }
			
			if (health <= 0)
			{			
				moveSpeed = 5
			}
			if (health < maxHealth / 3)
			{
				moveSpeed = 50;
			}
			else
			{
				moveSpeed = 20;
			}
			if (!dying)
			{
				if (velocity.x < 0) { velocity.x = -moveSpeed; }
				else { velocity.x = moveSpeed; }
			}
				
			if (x + width > GameState.FIELDLEFT + GameState.FIELDWIDTH + 30)
			{
				velocity.x = -moveSpeed;
			}
			else if (x < GameState.FIELDLEFT - 30)
			{
				velocity.x = moveSpeed;
			}
			
			thrusterTimer -= FlxG.elapsed;
			if (thrusterTimer <= 0)
			{
				thrusterTimer = 0.1;
				GameState.ref.makeParticle(Particle.THRUSTER, x + Math.random() * 10 + 40, y + Math.random() * 10 + 70);
				GameState.ref.makeParticle(Particle.THRUSTER, x + Math.random() * 10 + 60, y + Math.random() * 10 + 70);
			}
		}
		
		public function getHit(damage:Number):void
		{
			health -= damage;
			if (health <= 0 && stateTransition <= 0)
			{
				if (states.length > 1)
				{
					stateTransition = 1.5;
				}
				else
				{
					stateTransition = 5.0;
					dying = true;
					GameState.ref.bossDialog.showWin();
				}
				if (pattern)
				{
					pattern.patternEnd();
				}
				GameState.ref.resetBullets();
			}
			else if(health > 0)
			{
				y = 8;
				gotHit = true;				
				SoundAssets.hitBoss.Play();
			}
		}
		
		public function addState(state:BossState):void
		{
			states.push(state);
			if (!pattern)
			{
				health = (states[0] as BossState).health;
				maxHealth = health;
				pattern = (states[0] as BossState).pattern;
				pattern.patternStart();
			}
		}
		
		override public function postUpdate():void 
		{
			super.postUpdate();
			GameState.ref.bossShadow.x = x - 100;
			GameState.ref.bossShadow.y = y - 28;
		}
		
		override public function draw():void 
		{
			super.draw();
			
			var dx:Number = (x + width / 2) - (GameState.ref.player.x + GameState.ref.player.width / 2);
			var dy:Number = (y + height / 2) - (GameState.ref.player.y + GameState.ref.player.height / 2);
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			if (stateTransition > 0)
			{
				dx = Math.random() * 5 - 2.5 + 0.5;
				dy = Math.random() * 2 - 1;
			}
			else
			{
				dx *= -2.5 / dist;
				dy *= -1 / dist;
				dx += 0.5;
			}
			
			FlxG.camera.buffer.fillRect(new Rectangle(int(x) + int(eyeX + dx), int(y) + int(eyeY + dy), 2, 2), 0xff41000f);
			FlxG.camera.buffer.fillRect(new Rectangle(int(x) + int(eyeX + dx) + eyeDistance, int(y) + int(eyeY + dy), 2, 2), 0xff41000f);
		}
	}

}